package com.usesion.base.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usesion.base.page.PageDataInfo;
import com.usesion.base.page.PageParameter;
import com.usesion.base.page.PageSupport;
import com.usesion.base.shiro.ShiroUtils;
import com.usesion.entity.User;

import java.util.List;

/**
 * @desc: 通用controller
 * @author: XT
 */
public class BaseController {

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageParameter parameter = PageSupport.buildPageRequest();
        Integer pageNum = parameter.getPageNum();
        Integer pageSize = parameter.getPageSize();
        if (pageNum != null && pageSize != null) {
            String orderBy = parameter.getOrderBy();
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected PageDataInfo getPageData(List<?> list) {
        PageDataInfo pageInfo = new PageDataInfo();
        pageInfo.setRows(list);
        pageInfo.setTotal(new PageInfo(list).getTotal());
        return pageInfo;
    }

    public User getUser() {
        return ShiroUtils.getUser();
    }

    public void setUser(User user) {
        ShiroUtils.setUser(user);
    }

    public Integer getUserId() {
        return getUser().getId();
    }

    public String getUserName() {
        return getUser().getUserName();
    }

    /**
     * 带参重定向
     * @param path
     * @return
     */
    protected String redirect(String path) {
        return "redirect:" + path;
    }
}
